<html>
<head><meta charset="utf-8"><title>println macro prevents some optimizations? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/println.20macro.20prevents.20some.20optimizations.3F.html">println macro prevents some optimizations?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206354389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/println%20macro%20prevents%20some%20optimizations%3F/near/206354389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/println.20macro.20prevents.20some.20optimizations.3F.html#206354389">(Aug 08 2020 at 16:26)</a>:</h4>
<p>Hi,<br>
I would've imagined that rustc will optimize this:</p>
<div class="codehilite"><pre><span></span><code><span class="k">struct</span> <span class="nc">Hi</span><span class="p">([</span><span class="kt">usize</span><span class="p">;</span><span class="w"> </span><span class="mi">4</span><span class="p">]);</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">hi</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Hi</span><span class="p">([</span><span class="mi">1337</span><span class="p">,</span><span class="w"> </span><span class="mi">2048</span><span class="p">,</span><span class="w"> </span><span class="mi">1974</span><span class="p">,</span><span class="w"> </span><span class="mi">4235432543</span><span class="p">]);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">hi</span><span class="p">.</span><span class="mi">0</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>Into:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{}&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">1337</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>It seems that it doesn't optimize out all the dead stores and constants, but if I replace <code>println</code> with <code>printf</code>(or any other function for that matter) it actually does manage to optimize this<br>
<a href="https://godbolt.org/z/61Ydhj">https://godbolt.org/z/61Ydhj</a></p>



<a name="206355002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/println%20macro%20prevents%20some%20optimizations%3F/near/206355002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/println.20macro.20prevents.20some.20optimizations.3F.html#206355002">(Aug 08 2020 at 16:42)</a>:</h4>
<p>The formatting infrastructure does not optimize well in general</p>



<a name="206358004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/println%20macro%20prevents%20some%20optimizations%3F/near/206358004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/println.20macro.20prevents.20some.20optimizations.3F.html#206358004">(Aug 08 2020 at 18:12)</a>:</h4>
<p>(somewhat intentionally, we try to avoid inlining etc. Good for some use cases but not all)</p>



<a name="206358304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/println%20macro%20prevents%20some%20optimizations%3F/near/206358304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/println.20macro.20prevents.20some.20optimizations.3F.html#206358304">(Aug 08 2020 at 18:21)</a>:</h4>
<p>So I guess I should abstain from doing operations in the println macro? (and instead assign to a variable everything first)</p>



<a name="206360516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/println%20macro%20prevents%20some%20optimizations%3F/near/206360516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/println.20macro.20prevents.20some.20optimizations.3F.html#206360516">(Aug 08 2020 at 19:21)</a>:</h4>
<p>That shouldn't matter</p>



<a name="206363704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/println%20macro%20prevents%20some%20optimizations%3F/near/206363704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/println.20macro.20prevents.20some.20optimizations.3F.html#206363704">(Aug 08 2020 at 20:54)</a>:</h4>
<p>It looks like it does matter<br>
<a href="https://godbolt.org/z/v73bG7">https://godbolt.org/z/v73bG7</a></p>



<a name="206366543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/println%20macro%20prevents%20some%20optimizations%3F/near/206366543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/println.20macro.20prevents.20some.20optimizations.3F.html#206366543">(Aug 08 2020 at 22:21)</a>:</h4>
<p>See <a href="https://github.com/rust-lang/rust/issues/50519">https://github.com/rust-lang/rust/issues/50519</a>, I guess</p>



<a name="206529996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/println%20macro%20prevents%20some%20optimizations%3F/near/206529996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/println.20macro.20prevents.20some.20optimizations.3F.html#206529996">(Aug 11 2020 at 00:42)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> You can also put the operation inside braces (<a href="https://godbolt.org/z/9W7Gqc">https://godbolt.org/z/9W7Gqc</a>) to <a href="https://bluss.github.io/rust/fun/2015/10/11/stuff-the-identity-function-does/#rust-has-dedicated-syntax-for-this">force a move</a>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>